using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameLobby : MonoBehaviour
{
	public static GameLobby instance;

	public RectTransform playerListContentTransform;

	public GameObject warningDirectPlayServer;

	public Transform addButtonRow;

	public List<PlayerLobby> players = new List<PlayerLobby>();

	public PlayerLobby currentPlayerLobby;

	public InputField BotNumber;

	public Button StartButton;

	public Button ReadyButton;

	public GameObject ReadyIcon;
    private MyAdManager adManager;
	private string num="3";

    private void Start()
	{
        adManager = new MyAdManager();
        instance = this;
	}

	private void OnEnable()
	{
		instance = this;
	}

	public void DisconnectMatch()
	{
		if (!(currentPlayerLobby == null))
		{
			if (currentPlayerLobby.isServer)
			{
				UnityEngine.Debug.Log("stop lobby");
				UnitZ.gameNetwork.DestroyMatch();
                AdManager.Instance.ShowNative();
            }
			else
			{
				UnityEngine.Debug.Log("leave lobby");
				UnitZ.gameNetwork.LeaveMatch();
			}
		}
	}

	public void Update()
	{
		if (currentPlayerLobby == null)
		{
			return;
		}
		if (BotNumber != null)
		{
			//BotNumber.gameObject.SetActive(currentPlayerLobby.isServer);
			int.TryParse(BotNumber.text, out UnitZ.gameManager.BotNumber);
			if (UnitZ.gameManager.BotNumber > UnitZ.gameManager.BotMax)
			{
				UnitZ.gameManager.BotNumber = UnitZ.gameManager.BotMax;
			}
			BotNumber.text = UnitZ.gameManager.BotNumber.ToString();
		}
		if (currentPlayerLobby.isServer)
		{
			if ((bool)ReadyButton)
			{
				ReadyButton.gameObject.SetActive(value: false);
			}
			if ((bool)StartButton)
			{
				StartButton.gameObject.SetActive(value: true);
			}
		}
		else
		{
			if ((bool)ReadyButton)
			{
				ReadyButton.gameObject.SetActive(value: true);
			}
			if ((bool)StartButton)
			{
				StartButton.gameObject.SetActive(value: false);
			}
		}
		if (ReadyIcon != null)
		{
			ReadyIcon.SetActive(currentPlayerLobby.IsReady);
		}
	}

	public void StartMatch()
	{
		if (currentPlayerLobby != null && currentPlayerLobby.isServer)
		{
            UnitZ.gameNetwork.networkAddress = num;
            UnitZ.gameNetwork.AssignPlayersToSpawn(players.ToArray());
			UnitZ.gameNetwork.StartServerGame();
            adManager.MyShowNative();
        }
	}

	public void GetReady()
	{
		if (currentPlayerLobby != null)
		{
			currentPlayerLobby.OnReadyClicked();
		}
	}

	public void DisplayDirectServerWarning(bool enabled)
	{
		if (warningDirectPlayServer != null)
		{
			warningDirectPlayServer.SetActive(enabled);
		}
	}

	public void AddPlayer(PlayerLobby player)
	{
		if (!players.Contains(player))
		{
			players.Add(player);
			if (playerListContentTransform != null)
			{
				player.transform.SetParent(playerListContentTransform, worldPositionStays: false);
				RectTransform component = player.GetComponent<RectTransform>();
				RectTransform rectTransform = component;
				float width = playerListContentTransform.rect.width;
				Vector2 sizeDelta = component.sizeDelta;
				rectTransform.sizeDelta = new Vector2(width, sizeDelta.y);
				RectTransform rectTransform2 = playerListContentTransform;
				Vector2 sizeDelta2 = playerListContentTransform.sizeDelta;
				float x = sizeDelta2.x;
				float num = players.Count;
				Vector2 sizeDelta3 = component.sizeDelta;
				rectTransform2.sizeDelta = new Vector2(x, num * sizeDelta3.y);
			}
			if (addButtonRow != null)
			{
				addButtonRow.transform.SetAsLastSibling();
			}
			PlayerListModified();
		}
	}

	public void RemovePlayer(PlayerLobby player)
	{
		players.Remove(player);
		PlayerListModified();
	}

	public void PlayerListModified()
	{
		int num = 0;
		foreach (PlayerLobby player in players)
		{
			player.OnPlayerListChanged(num);
			num++;
		}
	}
}
